import ajax from '../../js/ajax.js';
import tools from '../../js/tools.js';

let ym = document.getElementById('ym');

let cs7;

let cs1 = {
  cid: -1,
  orderBy: 2,
  phone: '',
  qq: '',
  sname: '',
  wechat: ''
};
let cs2 = {
  pageNumber: 1,
  pageSize: 5
};
let nr = [];
//#region ==========学生信息获取=============
function xshq() {
  let cs = tools.concatJson(cs1, cs2);
  ajax.send('/manage/student/queryAll', cs, (Date) => {
    if (Date.success) {
      nr = Date.list;
      cs2=Date.page;
      xszs();
      bjhq();
      fy()
      return;
    }
    alert(Date.message);
  });
}
function xszs() {
  ym.innerHTML = '';
  for (let i = 0; i < nr.length; i++) {
    const et = nr[i];
    let tr = document.createElement('tr');
    let td;

    //学生编号
    td = document.createElement('td');
    td.append(et.sid);
    tr.append(td);
    //学生姓名
    td = document.createElement('td');
    td.append(et.sname);
    tr.append(td);
    //qq号
    td = document.createElement('td');
    td.append(et.qq);
    tr.append(td);
    //手机号码
    td = document.createElement('td');
    td.append(et.phone);
    tr.append(td);
    //家庭地址
    td = document.createElement('td');
    td.append(et.address);
    tr.append(td);
    //信息最后修改时间
    td = document.createElement('td');
    td.append(tools.FormData(et.lastupdate));
    tr.append(td);

    //删除按钮
    td=document.createElement('td');
    let an=document.createElement('button');
    an.append('删除');
    an.addEventListener('click',()=>{
      scff(et);
    })
    td.append(an);
    tr.append(td);

    //修改按钮
    td=document.createElement('td');
    let an1=document.createElement('button');
    an1.append('修改');
    an1.addEventListener('click',()=>{
      xgff(et);
    })
    td.append(an1);
    tr.append(td);
    ym.append(tr);
  }
}
//#endregion

//#region ==========学生分页==============
let fenye1 = document.querySelectorAll('nav>a');
let fenye2 = document.querySelector('nav>span');

function fy() {
  fenye2.innerHTML = `
  记录数/当前页/总页数
  ${cs2.total}/${cs2.pageNumber}/${cs2.pageCount}
  `;
}

fenye1[0].addEventListener('click', () => {
  if (cs2.pageNumber == 1) {
    return;
  }
  cs2.pageNumber = 1;
  xshq();
});
fenye1[1].addEventListener('click', () => {
  cs2.pageNumber--;
  if (cs2.pageNumber < 1) {
    cs2.pageNumber = 1;
    return;
  }
  xshq();
});
fenye1[2].addEventListener('click', () => {
  cs2.pageNumber++;
  if (cs2.pageNumber > cs2.pageCount) {
    cs2.pageNumber = cs2.cs2.pageCount;
    return;
  }
  xshq();
});
fenye1[3].addEventListener('click', () => {
  if (cs2.pageNumber == cs2.pageCount) {
    return;
  }
  cs2.pageNumber = cs2.pageCount;
  xshq();
});

//#endregion

//#region ==========联动==============
let bj=document.getElementById('bj');
let bjtc=document.getElementById('bjtc');
let bjxx=document.getElementById('bjxx');

bj.addEventListener('click',()=>{
  bjtc.showModal();
})
let cs6 = {
  cname: ''
};
let nr1=[];
function bjhq() {
  ajax.send('/manage/class/queryAll', cs6, (data) => {
    if (data.success) {
      nr1 = data.list;
      bjzs();
      return;
    }
    alert(data.message);
  });
}

function bjzs() {
  bjxx.innerHTML = '';
  for (let i = 0; i < nr1.length; i++) {
    const et = nr1[i];
    let tr = document.createElement('tr');
    let td;
    // 班级名称
    td = document.createElement('td');
    td.append(et.cname);
    tr.append(td);
    

    // 信息最后修改时间
    td = document.createElement('td');
    td.append(tools.FormData(et.lastupdate));
    tr.append(td);

    //选择按钮
    td=document.createElement('td')
    let an=document.createElement('button');
    an.append('选择');
    an.addEventListener('click',()=>{
      xzff();
    })
    td.append(an);
    tr.append(td);
    bjxx.append(tr);

    function xzff() {
      ajax.send('/manage/class/queryAll', et, () => {
        cs7=et;
        console.log('点击所需要的班级信息',cs7);
        bjhq();
      });
    }
  }
}


//#endregion

//#region ==========查询==============
let dhcx=document.getElementById('dhcx');
let qqcx=document.getElementById('qqcx');
let xmcx=document.getElementById('xmcx');
let wxcx=document.getElementById('wxcx');
let cx=document.getElementById('cx');
let cz=document.getElementById('cz');

cx.addEventListener('click',()=>{
  cs1.phone=dhcx.value;
  cs1.qq=qqcx.value;
  cs1.sname=xmcx.value;
  cs1.wechat=wxcx.value;
  xshq();
})
cz.addEventListener('click',()=>{
  dhcx.value='';
  qqcx.value='';
  xmcx.value='';
  wxcx.value='';
  cx.click();
})
//#endregion

//#region ==========增加==============
let zj=document.getElementById('zj');
let tjtc=document.getElementById('tjtc');
let dhtj=document.getElementById('dhtj');
let qqtj=document.getElementById('qqtj');
let xmtj=document.getElementById('xmtj');
let dztj=document.getElementById('dztj');
let wxtj=document.getElementById('wxtj');
let tj=document.getElementById('tj');
let bj1=document.getElementById('bj1');
let bjtc1=document.getElementById('bjtc1');
let bjxx1=document.getElementById('bjxx1');

bj1.addEventListener('click',()=>{
  bjtc1.showModal();
})
let nr2=[];
function bjhq1() {
  ajax.send('/manage/class/queryAll', cs6, (data) => {
    if (data.success) {
      nr2 = data.list;
      bjzs1();
      return;
    }
    alert(data.message);
  });
}

function bjzs1() {
  bjxx1.innerHTML = '';
  for (let i = 0; i < nr2.length; i++) {
    const et = nr2[i];
    let tr = document.createElement('tr');
    let td;
    // 班级名称
    td = document.createElement('td');
    td.append(et.cname);
    tr.append(td);
    
    // 信息最后修改时间
    td = document.createElement('td');
    td.append(tools.FormData(et.lastupdate));
    tr.append(td);

    //选择按钮
    td=document.createElement('td')
    let an=document.createElement('button');
    an.append('选择');
    an.addEventListener('click',()=>{
      xzff();
    })
    td.append(an);
    tr.append(td);
    bjxx1.append(tr);

    function xzff() {
      ajax.send('/manage/class/queryAll', et, () => {
        cs7=et;
        alert(cs7.cname);
        console.log('点击所需要的班级信息',cs7);
        bjhq();
      });
    }
  }
}
zj.addEventListener('click',()=>{ 
  bjhq1();
  tjtc.showModal();
})

tj.addEventListener('click',()=>{
  let tjnr={
    cid:cs7.cid,
    address:dztj.value,
    phone:dhtj.value,
    qq:qqtj.value,
    sname:xmtj.value,
    wechat:wxtj.value
  };
ajax.send('/manage/student/add',tjnr,(data)=>{
if(data.success){
  cs7.cid='';
  dztj.value='';
  dhtj.value='';
  qqtj.value='';
  xmtj.value='';
  wxtj.value='';
}
alert(data.message)
})
})


//#endregion

//#region ==========删除==============
function scff(j) {
  if (confirm(`是否删除：${j.cname}`)) {
    ajax.send('/manage/class/delete', { cid: j.cid }, (data) => {
      alert(data.message);
      cs2.pageNumber = 1;
      xshq();
    });
  }
}
//#endregion

//#region ==========修改==============
let xgtc=document.getElementById('xgtc');
let dhtj1=document.getElementById('dhtj1');
let qqtj1=document.getElementById('qqtj1');
let xmtj1=document.getElementById('xmtj1');
let bjid1=document.getElementById('bjid1');
let dztj1=document.getElementById('dztj1');
let wxtj1=document.getElementById('wxtj1');
let xsbh=document.getElementById('xsbh');
let xg=document.getElementById('xg');
function xgff(cs4) {
  dhtj1.value=cs4.phone;
  qqtj1.value=cs4.qq;
  xmtj1.value=cs4.sname;
  bjid1.value=cs4.cid;
  dztj1.value=cs4.address;
  wxtj1.value=cs4.wechat;
  xsbh.value=cs4.sid;
  xgtc.showModal();
}
xg.addEventListener('click', () => {
  let cs4 = {
    phone:dhtj1.value,
    qq:qqtj1.value,
    sname:xmtj1.value,
    cid:bjid1.value,
    address:dztj1.value,
    wechat:wxtj1.value,
    sid:xsbh.value
  };
  ajax.send('/manage/student/update', cs4, (data) => {
    alert(data.message);
  });
});

xgtc.addEventListener('close', () => {
  cs2.pageNumber = 1;
  xshq();
});
//#endregion



xshq();
